﻿using ERPEF.Model;
using ERPIService;
using Microsoft.EntityFrameworkCore;
using S4Project;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ERPService
{
    public class pur_Sales_ReturnService : pur_Sales_ReturnIService
    {
        private readonly MyDbContext MyDbContext;
        public pur_Sales_ReturnService(MyDbContext myDb)
        {
            this.MyDbContext = myDb;
        }

        //从表修改
        public async Task<int> FromUpdate(pur_Sales_From sales_From)
        {
            MyDbContext.pur_Sales_From.Update(sales_From);
            return await MyDbContext.SaveChangesAsync();
        }

        //主添加
        public async Task<int> ReturnAdd(pur_Sales_Return sales_Return)
        {
            await MyDbContext.pur_Sales_Return.AddAsync(sales_Return);
            return await MyDbContext.SaveChangesAsync();
        }

        //主修改
        public async Task<int> ReturnUpdate(pur_Sales_Return sales_Return)
        {
            MyDbContext.pur_Sales_Return.Update(sales_Return);
            return await MyDbContext.SaveChangesAsync();
        }


        // 根据订单编码获取采购订单
        public async Task<pur_Purchase_Order> GetByCode(string order)
        {
            return await MyDbContext.pur_Purchase_Order.FirstOrDefaultAsync(o => o.ord_number == order);
        }
        // 根据订单编码获取入库订单
        public async Task<pur_Chase_Receipt> GetByOutCode(string code)
        {
            return await MyDbContext.pur_Chase_Receipt.FirstOrDefaultAsync(o => o.pr_number == code);
        }

        // 更新采购订单信息
        public async Task Update(pur_Purchase_Order salesOrder)
        {
            MyDbContext.pur_Purchase_Order.Update(salesOrder);
            await MyDbContext.SaveChangesAsync();
        }

        // 更新采购订单信息
        public async Task UpdateOut(pur_Chase_Receipt chase_Receipt)
        {
            MyDbContext.pur_Chase_Receipt.Update(chase_Receipt);
            await MyDbContext.SaveChangesAsync();
        }
    }
}
